package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

//设置当前类为Spring的控制器类
//还有Model,View
@Controller
public class UserController111 {
    //设定当前方法的访问映射地址
    // @RequestMapping("/save")
    @RequestMapping("/save1")
    //跳转页面
    //设置当前方法返回值类型为String，用于指定请求完成后跳转的页面
    public String save1() {
        System.out.println("user mvc controller is running ...");
        //设定具体跳转的页面

        return "success.jsp";
    }

    //方法传递普通类型参数，数量任意，类型必须匹配
    //get的可以直接这样写参数上去
    @RequestMapping("/requestParam1")
    public String requestParam1(String name, Integer age) {
        System.out.println("name: " + name + "  age:  " + age);
        return "page.jsp";
    }

    @RequestMapping("/requestParam2")
    public String requestParam2(
            @RequestParam(value = "name", required = true, defaultValue = "aaa") String name,
            @RequestParam(value = "age", required = true, defaultValue = "11") int age) {
        System.out.println("name: " + name + "  age:  " + age);
        return "page.jsp";
    }

    //方法传递POJO类型参数，URL地址中的参数作为POJO的属性直接传入对象
    @RequestMapping("/requestParam3")
    public String requestParam3(User user) {
        System.out.println("name: " + user.getName() + "  age:  " + user.getAge());
        return "page.jsp";
    }

    //当方法参数中具有POJO类型参数与普通类型参数嘶，
    // URL地址传入的参数不仅给POJO对象属性赋值，也给方法的普通类型参数赋值
    @RequestMapping("/requestParam4")
    public String requestParam4(User user, Integer age) {
        System.out.println("name: " + user.getName() + "  age:  " + user.getAge() + " age " + age);
        return "page.jsp";
    }

    //方法传递POJO类型参数，URL地址中的参数作为POJO的属性直接传入对象
    //http://localhost/requestParam5?address.city=beijing
    @RequestMapping("/requestParam5")
    public String requestParam5(User user) {
        System.out.println("name: " + user.getName() + "  age:  " + user.getAge()
                + " address.city:  " + user.getAddress().getCity());
        return "page.jsp";
    }
    //通过URL地址中同名参数，可以为POJO中的集合属性进行赋值，集合属性要求保存简单数据

    //http://localhost/requestParam6?nick=Jock1&nick=Jockme&nick=zahc
    // http://localhost/requestParam6?nick[0]=Jock1&nick[1]=Jockme&nick[2]=zahc
    @RequestMapping("/requestParam6")
    public String requestParam6(User user) {
        System.out.println(user.getNick());
        return "page.jsp";
    }

    //POJO中List对象保存POJO的对象属性赋值，使用[数字]的格式指定为集合中第几个对象的属性赋值
    //http://localhost/requestParam7?addresses[0].city=beijing&addresses[1].province=hebei
    @RequestMapping("/requestParam7")
    public String requestParam7(User user) {
        System.out.println(" address.city:  " + user.getAddresses().get(0).getCity());
        return "page.jsp";
    }

    //POJO中Map对象保存POJO的对象属性赋值，使用[key]的格式指定为Map中的对象属性赋值
    //http://localhost/requestParam8?addressMap['job'].city=beijing&addressMap['home'].province=henan
    @RequestMapping("/requestParam8")
    public String requestParam8(User user) {
        System.out.println("addressMap:  " + user.getAddressMap());
        return "page.jsp";
    }

    //方法传递普通类型的数组参数，URL地址中使用同名变量为数组赋值
    @RequestMapping("/requestParam9")
    //http://localhost/requestParam9?nick=jack&nick=zahc
    public String requestParam9(String[] nick) {
        System.out.println("nick:  " + Arrays.toString(nick));
        return "page.jsp";
    }

    @RequestMapping("/requestParam10")
    //方法传递保存普通类型的List集合时，无法直接为其赋值，
    // 需要使用@RequestParam参数对参数名称进行转换
    //http://localhost/requestParam10?nick=Jockme&nick=zahc
    public String requestParam10(@RequestParam("nick") List<String> nick) {
        System.out.println("nick:  " + nick);
        return "page.jsp";
    }

    //数据类型转换，使用自定义格式化器或@DateTimeFormat注解设定日期格式
    //两种方式都依赖springmvc的注解启动才能运行
    //http://localhost/requestParam11?date=1999-09-09
    //通过springmvc配置文件中的converter子接口实现
    @RequestMapping("/requestParam11")
    public String requestParam11(Date date) {
        System.out.println("date:  " + date);
        return "page.jsp";
    }

    //
    @RequestMapping("/requestParam111")
    //http://localhost/requestParam111?date=1999-09-09
    public String requestParam111(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        System.out.println("date:  " + date);
        return "page.jsp";
    }

    @RequestMapping("/requestParam1111")
    //http://localhost/requestParam111?date=1999-09-09
    public String requestParam1111(User user) {
        System.out.println("date:  " + user.getDate());
        return "page.jsp";
    }

    @RequestMapping("/requestParam12")

    public String requestParam12(User user) {
        System.out.println("addressMap:  " + user.getAddressMap());
        return "page.jsp";
    }
}
